package com.chj.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.chj.constants.Constants;
import com.chj.dao.MessageDAO;
import com.chj.entity.Message;
import com.chj.mapper.MessageMapper;
import com.etc.jdbc.util.JDBCUtils;

@Repository
public class MessageDAOImpl implements MessageDAO{

    @Override
    public int addMessage(Message message) {

        String sql = "INSERT INTO `t_massage` (`massage_id`, `massage_msg`, `user_id`, `create_date`, `modify_date`, `delete_date`, `status`)";
               sql += "               VALUES  (?           , ?            , ?        , ?            , ?            , ?            , ?)";

        return JDBCUtils.update(sql, message.getMessageId(), message.getMessageMsg(), message.getUserId(), message.getCreateDate(), message.getModifyDate(), message.getDeleteDate(), message.getStatus());
    }

    @Override
    public int deleteMessage(String messageId) {

        String sql = "delete from t_massage where massage_id = ?";

        return JDBCUtils.update(sql, messageId);
    }

    @Override
    public List<Message> getAllMessage() {

        String sql = " select * from t_massage order by user_id, create_date desc";
        
        return JDBCUtils.queryForList(sql, new MessageMapper());
    }

    @Override
    public Message getMessageById(String messageId) {

        String sql = " select * from t_massage where message_id = ?";

        return JDBCUtils.queryForObject(sql, new MessageMapper(), messageId);
    }

    @Override
    public List<Message> getMessageListBy(String userId
                                        , String dateBegin
                                        , String dateEnd
                                        , String message
                                        , int page
                                        , int rows) {


        return null;
    }

    @Override
    public int getMessageListCountBy(String userId
                                   , String dateBegin
                                   , String dateEnd
                                   , String message) {

        
        return 0;
    }

    @Override
    public List<Message> getMessageListEqualsDateWithUser(String userId, String date) {

        String sql = " select * from t_massage where user_id = ? and create_date >= ? order by create_date";

        return JDBCUtils.queryForList(sql,new MessageMapper(), userId,date );
    }

    @Override
    public List<Message> getMessageListGatterDateWithUser(String userId, String date, int size) {

        String sql = " select * from t_massage where user_id = ? and create_date < ? order by create_date limit ?,? ";

        return JDBCUtils.queryForList(sql, new MessageMapper(), userId,date, 0,size);
    }

    @Override
    public int modifyMessage(Message message) {

        String sql = "UPDATE `t_massage` SET `massage_msg`= ?,`modify_date` = ? WHERE (`massage_id`=?)";

        return JDBCUtils.update(sql, message.getMessageMsg(), new Date(), message.getMessageId());
    }

    @Override
    public List<Message> getMsgListBy(String userId
                                    , String createDateFrom
                                    , String createDateTo
                                    , int page
                                    , int rows) {

        List<Object> paramsList = new ArrayList<Object>();

        String sql = "";

        sql += " select *                  ";
        sql += "   from t_massage t                 ";
        sql += "  where t.status = 1              ";

        if (StringUtils.isNotBlank(userId)) {
            sql += " and t.user_id like ? ";
            paramsList.add(userId+Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(createDateFrom)) {
            sql += " and t.create_date > ? ";
            paramsList.add(createDateFrom);
        }
        if (StringUtils.isNotBlank(createDateTo)) {
            sql += " and t.create_date < ? ";
            paramsList.add(createDateTo);
        }

        //定义数组长度
        int paramsLength = 0;
        if(paramsList.size() > 0) {
            paramsLength = paramsList.size();
        }

        //创建数组
        Object[] params = new Object[paramsLength];

        //赋值给数组
        for (Object object : paramsList) {
            int paramsIndex = paramsList.indexOf(object);
            params[paramsIndex] = object;
        }

        return JDBCUtils.queryForList(sql, new MessageMapper(), params);
    }

    @Override
    public int getMsgListBy(String userId
                          , String createDateFrom
                          , String createDateTo) {

        List<Object> paramsList = new ArrayList<Object>();

        String sql = "";

        sql += " select count(1)                  ";
        sql += "   from t_massage t               ";
        sql += "  where t.status = 1              ";

        if (StringUtils.isNotBlank(userId)) {
            sql += " and t.user_id like ? ";
            paramsList.add(userId+Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(createDateFrom)) {
            sql += " and t.create_date > ? ";
            paramsList.add(createDateFrom);
        }
        if (StringUtils.isNotBlank(createDateTo)) {
            sql += " and t.create_date < ? ";
            paramsList.add(createDateTo);
        }

        //定义数组长度
        int paramsLength = 0;
        if(paramsList.size() > 0) {
            paramsLength = paramsList.size();
        }

        //创建数组
        Object[] params = new Object[paramsLength];

        //赋值给数组
        for (Object object : paramsList) {
            int paramsIndex = paramsList.indexOf(object);
            params[paramsIndex] = object;
        }

        return JDBCUtils.queryForInt(sql, params);
    }

}
